package com.stone.gateway.config;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.HttpHandler;

/**
 * 网络监听
 * @author fxj
 * @date 2019/5/9
 */
@Configuration
public class NettyListener {

    @Autowired
    GwProperties gwProperties;

    @Resource
    HttpHandler httpHandler;

    WebServer http;

    @PostConstruct
    public void start(){
        if ( gwProperties.getPort() <= 0 ){
            return;
        }
        NettyReactiveWebServerFactory factory = new NettyReactiveWebServerFactory(gwProperties.getPort());
        factory.setSsl(gwProperties.getSsl());
        this.http = factory.getWebServer(httpHandler);
        this.http.start();
    }

    @PreDestroy
    public void stop(){
        this.http.stop();
    }
}
